import java.net.*;
import java.util.*;
import java.nio.*;
import java.nio.channels.*;
import java.io.IOException;



class Client
{

   Selector selector = null;


   public void doConnect(String hostname, int port)
   {
      InetSocketAddress serverAddr = new InetSocketAddress(hostname, port);
      try
      {
         selector = Selector.open();
         for (int i = 0; i < 1000; ++i)
         {
            SocketChannel channel = SocketChannel.open();
            channel.configureBlocking(false);
            SelectionKey token = channel.register(selector, SelectionKey.OP_CONNECT, new Integer(i + 1));
            channel.connect(serverAddr);
         }
      }
      catch (IOException e)
      {
         StringBuilder errorMessageBuilder = new StringBuilder();
         errorMessageBuilder.append("Could not connect to ").append(hostname).append( ": ").append(port).append(":").append(e.toString());

         System.out.println(new String(errorMessageBuilder));

      }
      try
      {
         while (selector.select() > 0)
         {
            Set acceptedKeys = selector.selectedKeys();
            Iterator i = acceptedKeys.iterator();
            while (i.hasNext()) {
               SelectionKey key = (SelectionKey)i.next();
               Integer iteration = (Integer)key.attachment();
               SocketChannel channel = (SocketChannel)key.channel();
               if (channel.isConnectionPending())
                  channel.finishConnect();
               Socket socket = channel.socket();

               if (! socket.isConnected())
               {
                  System.out.println("Not connected?  WTF?");
               }


               String message = new String(new StringBuilder()
                                           .append("Iteration ")
                                           .append(iteration)
                                           .append(" on port ")
                                           .append(socket.getPort())
                                           .append(" is connected.\n"));
               System.out.println(message);

               byte[] messageBytes = message.getBytes();
               ByteBuffer byteBuf = ByteBuffer.wrap(messageBytes);;

               channel.write(byteBuf);
               System.out.println("wrote to port");


               key.channel().close();


               i.remove();
            }

         }

      }
      catch (IOException e)
      {
         System.out.println(new StringBuilder()
                            .append("Could not write to ")
                            .append(hostname)
                            .append( ": ")
                            .append(port)
                            .append(":")
                            .append(e.toString()));
      }
      finally
      {
         try{
            selector.close();
         }
         catch (IOException e)
         {
         }

      }




   }

   public static void main(String[] args)
   {
      if (args.length == 2)
      {
         Client client = new Client();
         client.doConnect(args[0], Integer.parseInt(args[1]));
      }

   }
}
